﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;
using System.Text;

namespace IHISCETeim.Web.MachineSys.MacModel
{
    public partial class OQualityStrategyAdd : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.修改, Permission.新增 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "OQualityStrategyListEasyUI";
            }

        }
        protected string Action
        {
            get
            {
                return GetRequestValue<string>("action", "add");
            }
        }
        protected int QSID { get { return GetRequestValue<int>("QSID", 0); } }

        private OQualityStrategyInfo _OQualityStrategyObj = null;
        protected OQualityStrategyInfo OQualityStrategyObj
        {
            get
            {
                if (_OQualityStrategyObj == null)
                    _OQualityStrategyObj = OQualityStrategyController.GetByID(QSID);
                return _OQualityStrategyObj == null ? new OQualityStrategyInfo() { DuiCuBianHao="自动生成",MacType="" } : _OQualityStrategyObj;
            }
        }

        private List<OQualityStrategyAttachmentInfo> _listAttDetail = null;
        /// <summary>
        /// 所有附件明细
        /// </summary>
        protected List<OQualityStrategyAttachmentInfo> ListAttDetail
        {
            get
            {
                if (_listAttDetail == null)
                    _listAttDetail = OQualityStrategyController.GetListAttachmentByQSID(QSID);
                return _listAttDetail == null ? new List<OQualityStrategyAttachmentInfo>() { } : _listAttDetail;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "update")
            {
               updatemenu();
            }

            if (GetRequestValue<string>("ajaxmethod", "") == "add")
            {
                addmenu();
            }
            binddate();
        }
        protected void binddate()
        {
            string AttType = "duicireport";           
            // rpSendsvrBulletin
            AttType = "duicireport";
            List<OQualityStrategyAttachmentInfo>  _attschemelist = ListAttDetail.Where(e => e.AttType == AttType).ToList();
            if (_attschemelist.Count <= 0)
            {
                _attschemelist.Add(new OQualityStrategyAttachmentInfo() { ID = 0, AttType = AttType, QSID = QSID });
            }
            this.rpDuiceReportAtt.DataSource = _attschemelist;
            this.rpDuiceReportAtt.DataBind();

            AttType = "faultdescription";
             _attschemelist = ListAttDetail.Where(e => e.AttType == AttType).ToList();
            if (_attschemelist.Count <= 0)
            {
                _attschemelist.Add(new OQualityStrategyAttachmentInfo() { ID = 0, AttType = AttType, QSID = QSID });
                _attschemelist.Add(new OQualityStrategyAttachmentInfo() { ID = 0, AttType = AttType, QSID = QSID });
                _attschemelist.Add(new OQualityStrategyAttachmentInfo() { ID = 0, AttType = AttType, QSID = QSID });
            }
            this.rpfaultdescription.DataSource = _attschemelist;
            this.rpfaultdescription.DataBind();

            AttType = "causeanalysis";
            _attschemelist = ListAttDetail.Where(e => e.AttType == AttType).ToList();
            if (_attschemelist.Count <= 0)
            {
                _attschemelist.Add(new OQualityStrategyAttachmentInfo() { ID = 0, AttType = AttType, QSID = QSID });
                _attschemelist.Add(new OQualityStrategyAttachmentInfo() { ID = 0, AttType = AttType, QSID = QSID });
                _attschemelist.Add(new OQualityStrategyAttachmentInfo() { ID = 0, AttType = AttType, QSID = QSID });
            }
            this.rpcauseanalysis.DataSource = _attschemelist;
            this.rpcauseanalysis.DataBind();
           
        }

        protected void addmenu()
        {
            char[] caSplit = new char[] { ',' };
            string[] AttTypes = GetRequestValue<string>("AttType", "").Split(caSplit);
            string[] AttPaths = GetRequestValue<string>("AttPath", "").Split(caSplit);
            string[] Remarks = GetRequestValue<string>("Remark", "").Split(caSplit);

            OQualityStrategyInfo obj = Request.SerializableObject<OQualityStrategyInfo>();

            List<OQualityStrategyAttachmentInfo> list = new List<OQualityStrategyAttachmentInfo>();
            for (int index = 0; index < AttTypes.Count(); index++)
            {
                OQualityStrategyAttachmentInfo itm = new OQualityStrategyAttachmentInfo();
                itm.AttType = AttTypes[index];
                itm.AttPath = AttPaths[index];
                itm.Remark = Remarks[index];
                list.Add(itm);
            }

            int r = OQualityStrategyController.Add(obj, list);
            if (r == -1)
            {
                ErrorJson("添加失败");
            }
            if (r > 0)
            {
                SuccessJson("成功保存",this.DWZRef);
            }


        }

        protected void updatemenu()
        {
            char[] caSplit = new char[] { ',' };
            string[] IDs = GetRequestValue<string>("ID", "").Split(caSplit);
            string[] AttTypes = GetRequestValue<string>("AttType", "").Split(caSplit);
            string[] AttPaths = GetRequestValue<string>("AttPath", "").Split(caSplit);
            string[] Remarks = GetRequestValue<string>("Remark", "").Split(caSplit);

           // OQualityStrategyInfo obj = Request.SerializableObject<OQualityStrategyInfo>();
            OQualityStrategyObj.RelevantEntryNum = GetRequestValue<string>("RelevantEntryNum", "");
            OQualityStrategyObj.MacType = GetRequestValue<string>("MacType", "");
            OQualityStrategyObj.ItemCode = GetRequestValue<string>("ItemCode", "");
            OQualityStrategyObj.ItemName = GetRequestValue<string>("ItemName", "");
            OQualityStrategyObj.ProblemClass = GetRequestValue<string>("ProblemClass", "");
            OQualityStrategyObj.Frequency = GetRequestValue<string>("Frequency", "");
            OQualityStrategyObj.Tiems = GetRequestValue<string>("Tiems", "");
            OQualityStrategyObj.DuiceReport = GetRequestValue<string>("DuiceReport", "");
            OQualityStrategyObj.FaultDescription = GetRequestValue<string>("FaultDescription", "");
            OQualityStrategyObj.CauseAnalysis = GetRequestValue<string>("CauseAnalysis", "");

            List<OQualityStrategyAttachmentInfo> list = new List<OQualityStrategyAttachmentInfo>();
            for (int index = 0; index < IDs.Count(); index++)
            {
                if (string.IsNullOrEmpty(IDs[index]))
                    continue;
                OQualityStrategyAttachmentInfo itm = new OQualityStrategyAttachmentInfo();
                itm.ID = Convert.ToInt32(IDs[index]);
                itm.AttType = AttTypes[index];
                itm.AttPath = AttPaths[index];
                itm.Remark = Remarks[index];
                list.Add(itm);
            }

            bool r = OQualityStrategyController.Update(OQualityStrategyObj, list);
            if (r)
            {
                SuccessJson("成功保存", this.DWZRef);

            }
            else {
                ErrorJson("添加失败");
            }


        }
    }
}